package com.yonyou.iuap.corp.demo.yonbip.singlesignon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.corp.demo.yonbip.BaseTest;
//import jdk.internal.jline.internal.Nullable;
import lombok.Data;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @description: 单点登录YonBip/YonSuite
 * @author: nishch
 * @create: 2021-03-09
 **/
@Slf4j
public class SingleSignOn extends BaseTest {
    //获取临时code
    String sso_code = "https://api.diwork.com/diwork/yht/getThirdLoginCode";
    //单点登录拼接地址
    String openysurl = "https://euc.diwork.com/cas/thirdOauth2CodeLogin";
    //yonsuite访问地址--重定向到控制台
    String access_tet = "https://yonsuite.diwork.com/login";
    //yonsuite访问地址--重定向到指定节点
    String access_tet_node = "https://yonsuite.diwork.com/?effect=headless#/service/";

    //拼接最终请求地址
    String reqUrl = "%s?thirdUCId=%s&code=%s&service=%s";


    /**
     * 单点登录到工作台
     * @throws Exception
     */
    @Test
    public void singleSignTest() throws Exception {
        ThirdUserEntity entity = new ThirdUserEntity("nhbfpxn9","123456","nscer","nsc");
        String url = getSingleSignOnUrl(entity);
        oepnEexplorer(url);
    }

    /**
     * 单点登录到指定节点
     * @throws Exception
     */
    @Test
    public void singleSignNodeTest() throws Exception {
        ThirdUserEntity entity = new ThirdUserEntity("nhbfpxn9","123456","nscer","nsc","v7hr2yho","currency_u8c",null,null);
        String url = getSingleSignOnUrlToNode(entity);
        oepnEexplorer(url);
    }


    /**
     * 获取单点登录到指定节点地址
     * @param entity
     * @return
     * @throws Exception
     */
    public String getSingleSignOnUrlToNode(ThirdUserEntity entity) throws Exception {
        String code = getCode(entity);
        String thirdUCId = entity.getThirdUcId();
        //拼接访问系统的地址
        String node = URLEncoder.encode(access_tet_node+entity.getServiceCode(),"utf-8");
        String service = access_tet+"?tenantId="+entity.getTenantId()+"&service="+node;
        System.out.println("这是拼接的service地址"+service);
        String serviceUrl = URLEncoder.encode(service,"utf-8");
        //拼接最终单点地址
        return openysurl+"?thirdUCId="+thirdUCId+"&code="+code+"&service="+serviceUrl;
    }


    /**
     * 获取单点登录地址控制台地址
     * @param entity
     * @return
     */
    public String getSingleSignOnUrl(ThirdUserEntity entity) throws Exception {
        //集成认证中心-集成认证编码
        String thirdUCId = entity.getThirdUcId();
        String service = URLEncoder.encode(access_tet,"utf-8");
        String code = getCode(entity);
        return String.format(reqUrl, openysurl,thirdUCId,code,service);
    }


    public String getCode(ThirdUserEntity entity) throws Exception {
        if(null==entity||null==entity.getThirdUcId()|| StringUtils.isEmpty(entity.getThirdUcId())
                ||null==entity.getUserId()||StringUtils.isEmpty(entity.getUserId())){
            throw new Exception("用户参数不全");
        }
        String result = baseApi.doPostJson(sso_code, JSONObject.toJSONString(entity));
        Map map = JSON.parseObject(result,Map.class);
        //集成认证中心获取登录临时code
        Map<String,Object> data = (Map<String, Object>) map.get("data");
        return data.get("code").toString();
    }

    /**
     * 测试打开浏览器指定页面--模拟点击
     * @param url
     * @throws IOException
     */
    public static void oepnEexplorer(String url) throws IOException {
        try {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Data
    @Setter
    @Getter
    public static class ThirdUserEntity{

        public ThirdUserEntity(@NonNull String thirdUcId, @NonNull String userId, @NonNull String userName, @NonNull String userCode) {
            this.thirdUcId = thirdUcId;
            this.userId = userId;
            this.userName = userName;
            this.userCode = userCode;
        }

        public ThirdUserEntity(@NonNull String thirdUcId, @NonNull String userId, @NonNull String userName, @NonNull String userCode, @Nullable String mobile, @Nullable String email) {
            this.thirdUcId = thirdUcId;
            this.userId = userId;
            this.mobile = mobile;
            this.email = email;
            this.userName = userName;
            this.userCode = userCode;
        }


        public ThirdUserEntity(@NonNull String thirdUcId, @NonNull String userId, @NonNull String userName, @NonNull String userCode, @NonNull String tenantId, @NonNull String serviceCode,@Nullable String mobile, @Nullable String email) {
            this.thirdUcId = thirdUcId;
            this.userId = userId;
            this.mobile = mobile;
            this.email = email;
            this.userName = userName;
            this.userCode = userCode;
            this.tenantId = tenantId;
            this.serviceCode = serviceCode;
        }

        private String thirdUcId;  //集成认证应用编码
        private String userId;   //第三方用户id
        private String mobile;  //第三方手机号
        private String email;   //第三方邮箱
        private String userName;  //第三方用户名
        private String userCode;  //第三方用户编码
        //单点等指定节点需要用到------------------------
        private String tenantId;   //要登录的租户id
        private String serviceCode;  //单点节点的serviceCode
    }

}
